package com.example.springjpa.service;

import com.example.springjpa.pojo.User;
import com.example.springjpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author 杰哥
 * @Title: UserService
 * @ProjectName springjpa
 * @Description: TODO
 * @date 2019/8/99:31
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> findByAll() {
        List<User> all = userRepository.findAll();
        return all;
    }

    public User insert(User user) {
        User save = userRepository.save(user);
        return save;
    }

    public Integer delete(Integer id) {
        Integer restul = null;
        try {
            userRepository.deleteById(id);
            restul = 1;
        }catch (Exception e){
            e.printStackTrace();
            restul = 0;
        }
        return restul;
    }

    public User update(User user) {

        return userRepository.save(user);
    }
    public User findById(Integer id) {
        User user = userRepository.findById(id).get();
        return user;
    }
    public List<User> findByName(String username) {
        return  userRepository.findByUsername(username);

    }
}
